import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import { getHouseDetails } from "../api/index";

export default new Vuex.Store({
  state: {
    token: "",
    user_id: "",
    user_name: '',
    num: 0,
    // home页初始化参数
    params: {
      city: '武汉',
      community: '',
      pagenum: 1,
      pagesize: 10
    },
    // 搜索参数
    searchParams: {
      city: '武汉',
      area: '',
      community: '',
      pagenum: 1,
      pagesize: 150
    },
    // 房源详情信息
    currentHouseDetails:{},
    // 房源详情页跳转基准路径
    prevPage: '/home',
    // 订单提交前存储商品信息
    goodsList: [],
    // 商品详情信息
    currentGoods:{},
    checkedRadio: '',
    checkedBox: []
  },
  mutations: {
    // vuex 存储token

    setNum(state,payload) {
      state.num = payload;
    },
    setToken(state, payload) {
      state.token = payload;
      window.localStorage.setItem("myToken", payload);
    },
    setUserId(state, payload) {
      state.user_id = payload;
      window.localStorage.setItem("user_id", payload);
    },
    userName(state,payload){
      state.user_name = payload;
      window.localStorage.setItem("user_name", payload);
    },
    clearToken(state) {
      window.localStorage.removeItem("myToken");
    },
    setCity(state, payload) {
      window.localStorage.setItem("city", payload);
      state.params = {
        city: payload,
        community: '',
        pagenum: 1,
        pagesize: 10
      }
    },
    uploadParams(state, payload) {
      state.params = payload
    },
    uploadsearchParams(state, payload) {
      state.searchParams = payload
    },
    // 记录房子详情的上一页
    setPrevPage(state,payload) {
      state.prevPage = payload
    },
    setGoodsList(state, goods) {
      state.goodsList = goods
      // 商品信息存储到本地
      window.localStorage.setItem("goodsList", JSON.stringify(goods));
    },
    setCurrentGoods(state,  goods) {
      state.currentGoods = goods
      window.localStorage.setItem('currentGoods', JSON.stringify(goods))
    },
    setCheckedRadio(state,payload) {
      state.checkedRadio = payload
    },
    setCheckedBox(state,payload) {
      state.checkedBox = payload
    },
  },
  actions: {
    // 房源详情页
    async houseDetails(state,params) {
      // console.log(params);
      let res = await getHouseDetails(params[0])
      state.currentHouseDetails = res;
      window.localStorage.setItem("currentHouse", params[0]);
      window.localStorage.setItem("currentHouseName", params[1]);
    }

  },


  getters: {
    getToken(state) {
      if (state.token) {
        return state.token;
      }
      return localStorage.getItem("myToken");
    },
    getUsername(state) {
      if (state.user_name) {
        return state.user_name;
      }
      return localStorage.getItem("user_name");
    },
  },
  
});
